#include "imcommon.h"
//#include "XmlStorer.h"

char *clear_gtalkcontent(char *source)
{
    if (source == NULL)
        return NULL;
    char *str = strdup(source);
    int result;
    if (result != -1)
        result = cns_str_ereplace(&str, "</body>.*?<body>", "...\n");
    return str;
}

char *deal_friends(char *source)
{   
    if (source == NULL)
        return NULL;
    char *str = strdup(source);
    int result;
    if (result != -1)
        result = cns_str_ereplace(&str, "subscription=\".*?\"", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "ros:.*?=\".*?\"", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "<item jid", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "=", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "/>", ",");
    if (result != -1)
        result = cns_str_ereplace(&str, " ", "");
    if (result != -1)
        result = cns_str_ereplace(&str, "\"", "");
    return str;
}


    
int get_gtalk_friends(MsgNode *gtalk_node)
{
/*
    < iq to="XiYingIT@gmail.com/Talk.v1078BDC4A9A" id="16" type="result">
    <query ros:ext="2" xmlns="jabber:iq:roster" xmlns:ros="google:roster">
    <item jid="kenshinittest@gmail.com" subscription="both" ros:w="6" ros:mc="26"/>
    <item jid="554101587@qq.com" subscription="none" ros:w="0" ros:emc="1"/>
    <item jid="fengjie1051@gmail.com" subscription="both" ros:w="0" ros:mc="2"/>
    <item jid="huangb0910200213@gmail.com" subscription="both"/></query></iq>
*/
    char *pstart = NULL;
    char *pend   = NULL;
    
    char patternfriends[] = "<item jid=(.*)/>";
    //printf("data:%s\n",gtalk_node->data);

    char *strtmp = NULL;
    regcompile_2(gtalk_node->data, patternfriends, &strtmp);
    if (NULL == strtmp)
        return -1;
    gtalk_node->friends = deal_friends(strtmp);
    strtmp = NULL;

    char *p = NULL;

    p = str_in_node((char **)&gtalk_node->username, MAX_UN_LEN, gtalk_node->data, gtalk_node->real_dataLen, "<iq to=\"", "@gmail.com", 8, 10) ;
    if (NULL == p)
        return -1;
    gtalk_node->Id = NULL;
    p = str_in_node(&gtalk_node->Id, 0, p, gtalk_node->real_dataLen, "id=\"", "\"", 4, 0) ;
    if (NULL == p)
        return -1;
    memset(gtalk_id, 0, 40);
    memcpy(gtalk_id, gtalk_node->Id, pend-pstart+1);
    
    gtalk_node->msgType = Login;
    output_msg(gtalk_node);
    memset(gtalk_node->username, 0, MAX_UN_LEN);
 //   free(strtmp);
    free(gtalk_node->Id);
    gtalk_node->Id = NULL;
    gtalk_node->friends = NULL;
    return 0;
}

int analyse_gtalk_Login( MsgNode *gtalk_node)
{
     /*
    <iq id="0" type="result"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
    <jid>Kenshinittest@gmail.com/Talk.v107830308E3</jid></bind></iq>
     */
  
    char *p = NULL;
    p = str_in_node((char **)&gtalk_node->username, MAX_UN_LEN, gtalk_node->data, gtalk_node->real_dataLen, "<jid>", "@gmail.com", 5, 10) ;

    gtalk_node->Id= (char *)malloc(2);
    memcpy(gtalk_node->Id,"0",1);

    memset(gtalk_id, 0, 40);
    memcpy(gtalk_id, gtalk_node->Id, strlen(gtalk_node->Id));
    
    gtalk_node->msgType = Login;
  
   usr_record *puser = search_user(user_record,gtalk_node->destMac, "@gmail.com");
    if (puser != NULL)
        del_user(puser);

    insert_user(user_record,gtalk_node->destMac, gtalk_node->username);
    output_msg(gtalk_node);
    free(gtalk_node->Id);
    gtalk_node->Id = NULL;
    return 0;
}

int analyse_gtalk_Logout( MsgNode *gtalk_node)
{
    static int Logout_flag = 0;
    usr_record *p = search_user(user_record,gtalk_node->srcMac, "@gmail.com");
    if (NULL != p)
        strcpy(gtalk_node->username,user_record->username);
    gtalk_node->msgType = Logout;
    struct tm *ptm = localtime(&(gtalk_node->time));
    if (++Logout_flag == 1)
    {
        printf("%s is exiting\n",gtalk_node->username);
        printf("exited time is:%u-%u-%u %u:%02u\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,ptm->tm_hour, ptm->tm_min);
        if (NULL != p)
            del_user(p);
        return 0;
    }
    if(Logout_flag == 4)
    {
        printf("%s exited!\n",gtalk_node->username);
        printf("exited time is:%u-%u-%u %u:%02u\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,ptm->tm_hour, ptm->tm_min);
        output_msg(gtalk_node);
    if (NULL != p)
            del_user(p);
    }
    
    return 0;
}

int write_gtalk_msg(MsgNode *gtalk_node)
{
/*
(?<=<message)(.*?<body>.*)(?=\</message>) 
  */
  char patterndata[]   = ".*<message(.*?)<body>";
  char patterndata1[] = "<body>(.*)</body>";
  char patterndata2[] = "</body>(.*?)</message>";

    int id_flag = 0;

    char *tmp = NULL;
    regcompile_2(gtalk_node->data, patterndata, &tmp);
    if (NULL == tmp)
        return -1;
    regcompile_2(gtalk_node->data, patterndata1, &(gtalk_node->content));
    if (NULL == gtalk_node->content)
        return -1;
    char *tmp_content = NULL;
    tmp_content = clear_gtalkcontent(gtalk_node->content);
    if (NULL == tmp_content)
        return -1;
    free(gtalk_node->content);
    gtalk_node->content = clear_html_symbol(tmp_content);
    if (NULL == gtalk_node->content)
        return -1;
    tmp_content = NULL;
    char *p = NULL;
    p = str_in_node((char **)&gtalk_node->to, MAX_TO_LEN, tmp, gtalk_node->real_dataLen, "to=\"", "@gmail.com", 4, 10) ;
    if (NULL == p)
        return -1;
    char *p1 = p;
    gtalk_node->Id = NULL;
    p = str_in_node(&gtalk_node->Id, 0, p, gtalk_node->real_dataLen, "id=\"", "\"", 4, 0) ;
    if (NULL == p)
    {
        gtalk_node->is_offline_msg = 1;
        p = p1;
    }
    else
    {
        id_flag = 1;
        gtalk_node->is_offline_msg = 0;
        memset(gtalk_id, 0, 40);
        memcpy(gtalk_id, gtalk_node->Id, strlen(gtalk_node->Id));
    }

    
    p = str_in_node((char **)&gtalk_node->from, MAX_FROM_LEN, p, gtalk_node->real_dataLen, "from=\"", "@gmail.com", 6, 10) ;
    if (NULL == p)
    {
        gtalk_node->s_or_r = 0;
        usr_record *puser = search_user(user_record,gtalk_node->srcMac, "@gmail.com");
        if (puser)
        {
            memcpy(gtalk_node->from, puser->username, strlen(puser->username)+1);
            memcpy(gtalk_node->username,puser->username, strlen(puser->username)+1);
        }
        p = p1;
    }
    else
    {
        gtalk_node->s_or_r = 1;
        memcpy(gtalk_node->username, gtalk_node->from, strlen(gtalk_node->from)+1);
    }
    if (1 == gtalk_node->is_offline_msg)
    {
        char *tmp2 = NULL;
        regcompile_2(gtalk_node->data, patterndata2, &tmp2);

        if (NULL == tmp2)
            return -1;
        char *tmp_time = NULL;
        p = str_in_node(&tmp_time, 0, tmp2, gtalk_node->real_dataLen, "<x stamp=\"", "\"", 10, 0) ;
        if (NULL == p)
            return -1;

        gtalk_node->lasttime= str_to_time(tmp_time,  0,  4,  6,  9, 12, 15,
                                                                                            0,  0,  0,  8,  0,   0);
        free(tmp_time);
    }
    gtalk_node->msgType = Text;
    output_msg(gtalk_node);
    if (1 == id_flag)
    {
        free(gtalk_node->Id);
        id_flag = 0;
    }
    
    return 0;
}

